/*LICENSE_BEGIN
 * ******************************************************************
 * Copyright 2006 by the Dainamite Robocup-Team at the
 * DAI-Labor, TU-Berlin (www.dai-labor.de)
 * 
 * Members of Dainamite are:
 * Holger Endert
 * Robert Wetzker
 * Thomas Karbe
 * Axel Heßler
 * Philippe Bütter
 * Felix Brossmann
 * 
 * Licensed under the GNU GPL (General Public License) Free Software 
 * License, Version 2. You may use this file only in compliance with 
 * the License. You may obtain a copy of the License at:
 * 
 * http://www.fsf.org/licensing/licenses/gpl.txt
 * ******************************************************************
LICENSE_END*/
package robocup.component.tactics;

import robocup.component.actionfactories.BasicActionFactory;
import robocup.component.geometry.Vektor;

/**
 * State that moves a player to his start-position when in play-mode
 * before kick-off. The start-position depends on the number of the player
 * and is encoded into the FormationData-class.
 */
public class StartPositionState extends AbstractState {

	private Vektor posKickOffOther = null;

	private Vektor posKickOffOwn = null;

	/**
	 * The state to beam to the start-position!
	 * 
	 * @param parent -
	 *            the StateEvaluation (parent) object
	 */
	public StartPositionState(StateEvaluation parent) {
		super(parent);
		this.state = STATES.START_POSITION;
	}

	/**
	 * The precondition consits if the correct playmode and if the player is
	 * near the position, where he wants to beam to.
	 */
	@Override
	public boolean preCondition() {

		this.posKickOffOther = FormationData.getStartPosition(getWorld()
				.getSelfRef().getDefaultRole(), false);
		this.posKickOffOwn = FormationData.getStartPosition(getWorld()
				.getSelfRef().getDefaultRole(), true);

		if (!getWorld().hasMoved() && checkPlayMode())
			return true;

		Vektor nearPos;
		if (getWorld().isKickOffOwn())
			nearPos = this.posKickOffOwn;
		else {
			nearPos = this.posKickOffOther;
		}
		Vektor myPos = getWorld().getSelfRef().getPosition().cloned();
		boolean near = myPos.getDistance(nearPos) < 2.0;
		if (myPos.x == 0 && myPos.y == 0)
			near = false;
		return (checkPlayMode() && !near);
	}

	/**
	 * Success is always given in the given Play_Modes!
	 */
	@Override
	protected double successProbability() {

		return 1;
	}

	/**
	 * Benefit is also maximum, there is no better place!
	 */
	@Override
	protected double successBenefit() {

		return 1;
	}

	@Override
	public void calculateMainAction() {
		if (getWorld().isKickOffOwn())
			mainAction = BasicActionFactory.move(this.posKickOffOwn);
		else
			mainAction = BasicActionFactory.move(this.posKickOffOther);
	}
}
